<?php
namespace Home\Controller;

use Think\Controller;

class IndexController extends Controller
{
    private function _common_data($rItem, $sItem)
    {
        $roomModel = D('Room');
        $this->assign('action_name',ACTION_NAME);
        $this->assign('recommend', $roomModel->relation(true)->where('recommend=1')->limit($rItem)->select());
        $this->assign('schedule', $roomModel->relation(true)->order('createtime desc')->limit($sItem)->select());
    }

    public function index()
    {
        $roomModel = D('Room');

        self::_common_data(8, 6);
        $this->assign('hot', $roomModel->relation(true)->order('clickcount desc')->limit(9)->select());
        $this->assign('teacher', M('user')->where('tflag=1')->limit(6)->select());
        $this->assign('schools', M('school')->limit(6)->select());
        $this->assign('course', M('course')->limit(12)->select());
        $this->display();
    }

    /**
     * 不筛选 综合排序情况下，按照创建时间降序输出
     *
     * 观看人数按照clickcount来排序
     *
     * 筛选数据储存在session中
     */
    public function online_class()
    {
        session('case', null);
        if (empty(session('case'))) {
            $case = array();
        } else {
            $case = session('case');
        }

        if (!empty(I('filter_school'))) {
            if (I('filter_school') === '0') {
                unset($case['filter_school']);
            } else {
                $school = json_decode(htmlspecialchars_decode(I('filter_school')));
                $case['filter_school'] = $school;
                $where['school_id'] = array('in', $school);
            }
        } else if (!empty($case['filter_school'])) {
            $where['school_id'] = array('in', $case['filter_school']);
        }

        if (!empty(I('sort'))) {
            $condition = I('sort');
            if ($condition == 'auto') {
                $sort = 'createtime desc';
            }

            if ($condition == 'count') {
                $sort = 'clickcount desc';
            }
            $case['sort'] = I('sort');
        } else {
            $case['sort'] = 'auto';
            $sort = 'createtime desc';
        }

        if (!empty(I('grade'))) {
            $case['grade'] = I('grade');
            $where['grade'] = I('grade');
        } else if (!empty($case['grade'])) {
            $where['grade'] = $case['grade'];
        }

        if (!empty(I('filter_course'))) {
            $case['filter_course'] = I('filter_course');
            $where['courseID'] = I('filter_course');
        } else if (!empty($case['filter_course'])) {
            $where['courseID'] = $case['filter_course'];
        }

        session('case', $case);

        $roomModel = D('Room');
        $count = M('room')->where($where)->count();
        $Page = new \Think\Page($count, 15);    //实例化分页类 传入总记录数和每页显示的记录数(15)
        $show = $Page->show();    //分页显示输出
        $rooms = $roomModel->relation(true)->where($where)->order($sort)->limit($Page->firstRow . ',' . $Page->listRows)->select();    //分页查询

        if (!empty($case['filter_school'])) {
            $w['id'] = array('in', $case['filter_school']);
            $filterSchool = M('school')->where($w)->select();
        }

        $this->assign('ss', $filterSchool);
        $this->assign('page', $show);    //赋值分页输出
        $this->assign('rooms', $rooms);
        $this->assign('course', M('course')->limit(12)->select());
        $this->assign('case', $case);
        self::_common_data(4, 6);
        $this->display();
    }

    public function getSchools()
    {
        $where['grade'] = I('grade');
        if (I('grade') == 'allschool') {
            unset($where);
        }

        $count = M('school')->where($where)->count();
        $Page = new \Think\Page($count, 6);    //实例化分页类 传入总记录数和每页显示的记录数(15)
        $show = $Page->show();

        $show = str_replace('href', 'info', $show);
        $school = M('school')->where($where)->limit($Page->firstRow . ',' . $Page->listRows)->select();    //分页查询
        $this->ajaxReturn(array(
            'schools' => $school,
            'page' => $show
        ), 'json');
    }
}